<?php
namespace Home\Controller;
header("Content-type: text/html; charset=utf-8");
use Common\Lib\User;
use Home\Model\DepartmentModel;
use Home\Model\RoleModel;

class CrmEmployeeController extends CommonController {
	protected $table="RbacUser";
	protected $pageSize = 13;


	public function index (){

		   //获取所有CRM部门 (一维)
        $getalldepartmentsList_url = "Department/getAllDepartmentsList";
	    $alldepartmentsList = json_decode(http_get($getalldepartmentsList_url),true);
		
        //获取所有CRM部门 (二维)
        $getalldepartments_url = "Department/getAllDepartments";
	    $alldepartments = json_decode(http_get($getalldepartments_url),true);
	    //获取所有CRM职能 (一维)
	    $getallroles_url = "Role/getAllRolesList";
	    $crm_allrolesList = json_decode(http_get($getallroles_url),true);
	    //获取所有CRM职能 (二维)
	    $getallroles_url = "Role/getAllRoles";
	    $crm_allroles = json_decode(http_get($getallroles_url),true);
	     //获取所有省市区(一维)
        $getallareaList_url = "Area/getAllAreaList";
	    $crm_allareaList = json_decode(http_get($getallareaList_url),true);
	  



	/*	$this->assign("roleList", $this->getRoles());*/
		$this->assign("groupList", D('Group')->where(array('status'=>1))->select());
		$this->assign("sexType", array("未定义", "男", "女"));
		$this->assign("marriageTypeList", array("未定义", "未婚", "已婚"));
		$ename = $this->getRoleEname();
    	$this->assign('viewDecorator', $this->M->decoratorView($ename));

//非人资中心的人事专员只允许他看到所在部门的CRM员工  开始
   /* 	//部门选项权限
    	$ename=$this->getRoleEname();

        //所属部门
        $arr=D('Department')->where(array('id'=>session('account')['userInfo']['department_id']))->field('id,name,type')->select();
    	if($ename==RoleModel::GOLD||$ename==RoleModel::HR||$ename==RoleModel::HR_MASTER){
    	    //总经办、人事经理、人事专员权限
            if($ename==RoleModel::HR&&$arr[0]['type']!='3'){
                $ar=D('Group')->getAllGoups(session('account')['userInfo']['department_id'],'id,name');
                $departments=array('list'=>$arr,'account'=>$arr,'group'=>$ar);
            }else{
                $departments=array('list'=>D('Department')->getAllDepartments('id,name'),'account'=>'','group'=>'');
            }
        }else{
    	    //部门经理权限
            //部门所属团队小组
            $ar=D('Group')->getAllGoups(session('account')['userInfo']['department_id'],'id,name');
            $departments=array('list'=>$arr,'account'=>$arr,'group'=>$ar);
        }*/
        //改写 开始
        //部门选项权限
    	$ename=$this->getRoleEname();
    	 //所属部门
    	$departmentInfo = D("Department")->getDepartment(session('account')['userInfo']['department_id']);
        $arr=$departmentInfo["list"];
        
        if($ename==RoleModel::GOLD||$ename==RoleModel::HR_MASTER || session('account')['userInfo']['department_id']==22){
        	 $departments=array('list'=>$alldepartments["list"],'account'=>'','group'=>'');    
        }else{
        	    //根据部门id获取所有组 (二维)
        	    $department_id=session('account')['userInfo']['department_id'];
        	    $field = "id,name";
			    $getgroupById_url = "Group/getGroupById?id=".$department_id."&field=".$field;
			    $crm_groups = json_decode(http_get($getgroupById_url),true);
                $departments=array('list'=>$arr,'account'=>$arr,'group'=>$crm_groups);
        }
	    
        
        
        //改写 结束
//非人资中心的人事专员只允许他看到所在部门的CRM员工  结束

     
	   
   

    	$this->assign('departments',$departments);
    	$this->assign('allDepartmentsList',$alldepartmentsList["list"]);//一维
    	//$this->assign('allDepartments',$alldepartments["list"]);//二维
    	$this->assign('depart_id', $this->getDepartmentId());
    	$this->assign('departmentItem', $this->setEmployeeDepartemtnItem());
		
    	/*$this->assign('allRoles', D('Role')->getField('id,name', true));
    	$this->assign("roleList", $this->getRoles());*/
    	$this->assign('allRolesList', $crm_allrolesList["list"]);//一维数组
    	$this->assign("allRoles", $crm_allroles["list"]);//二维数组
    	$this->assign('allAreaList', $crm_allareaList["list"]);//一维数组
    	$creator = $_SESSION['account']['userInfo']['realname'];
    	$this->assign('creator',$creator);
		$this->display();
	}
	//使用接口文件调用crm员工的查询方式
	public function getList(){
		$department_id = $_GET["department_id"];
		if(session('account')['userInfo']['department_id']!=22){
			$department_id = session('account')['userInfo']['department_id']; 
		}
		$url = "User/getAllUser?p=".$_GET["p"]."&status=".$_GET["status"]."&department_id=".$_GET["department_id"]."&group_id=".$_GET["group_id"]."&type=".$_GET["type"]."&typeValue=".$_GET["typeValue"];
		//$url = "User/getAllUser?p=".$_GET["p"]."&status=".$_GET["status"];
		if(is_numeric($department_id)&&$department_id!=0){
			$url = $url."&department_id=".$department_id;
		}
		if(is_numeric($_GET["group_id"])&&$_GET["group_id"]!=0){
			$url = $url."&group_id=".$_GET["group_id"];
		}
		if($_GET["type"]!="" && $_GET["typeValue"] !=""){
			$url = $url."&type=".$_GET["type"]."&typeValue=".$_GET["typeValue"];
		}


		//$res = http_get("User/getAllUser", I("get."));
		$res = http_get($url);

	
		$json_data = json_decode($res, true);
        $result = array('list'=>$json_data["data"]["list"], 'count'=>$json_data["data"]["count"]);
        $this->ajaxReturn($result);
	}
//原来的不使用接口文件调用crm员工的查询方式
/*	public function setQeuryCondition() {
		
        //exit();
		$this->M->join('user_info ON rbac_user.id = user_info.user_id')
				->join('department_basic as db on user_info.department_id=db.id', 'left')
		        ->field('db.name as department_name,account,user_info.address,
		        	area_city,area_district,
		        	area_province,created_at,
		        	department_id,group_id,
		        	head,rbac_user.id,mphone,no_authorized,phone,
		        	qq,qq_nickname,realname,role_ename,role_id,sex,rbac_user.status,user_info.user_id,weixin,
		        	weixin_nikname,id_card,card_img,card_front,card_back,ip,location,lg_time,out_time')->where(array('no_authorized'=>0))
		        ->where(array('rbac_user.status'=>I('get.status')));
        if (isset($_GET['department_id'])) {
            $this->M->where(array('department_id'=>$_GET['department_id']));
        }

        if (isset($_GET['group_id'])) {
            $this->M->where(array('group_id'=>$_GET['group_id']));
        }
		$this->setRoleCondition();
		if (isset($_GET['type']) && isset($_GET['typeValue'])) {
			$this->M->where(array(I('get.type')=>array('like', I('get.typeValue')."%")));
		}	
		$this->M->where(array('rbac_user.id'=>array('neq', session('uid'))));
	}*/

	private function getDepartmentRow(){
		if (!isset($this->departmentRow)) {
			$this->departmentRow = D('Department')->find($this->getDepartmentId());
		}
		return $this->departmentRow;
	}

	private function isHrDeparment(){
		$this->getDepartmentRow();
		
		if ($this->departmentRow['type'] == DepartmentModel::HR_DEPARTMENT) {
			return true; //$_POST['department_id'] = 0;
		} else {
			return false; //$_POST['department_id'] = session('account')['userInfo']['department_id'];
		}
	}

	private function getDepartmentId(){
		// var_dump(session('account')['userInfo']['department_id']);
		if ($this->getRoleEname()=='gold') {
			return 0;
		} else {
			$depart_id = session('account')['userInfo']['department_id'];
			
			if ($depart_id == 0) {
				return 9999999;
			} else {
				return $depart_id;
			}
		}
		
	}

	

	private function setDeparmentQuery(){
		$departmentRow = $this->getDepartmentRow();
		$config = json_decode($departmentRow['config'], true);
		if (isset($config['EmployeeQueryCondition'])) {

			call_user_func(array($this, 'set'.$config['EmployeeQueryCondition']));
		}
	}



	private function setDepartmentEmployee(){
		
		$this->M->where(array(
			'user_info.department_id'=>array('eq', $this->getDepartmentId()),
			// 'role_id'=>array('NEQ', array()) 
		));

	}
	private function setAllEmployee(){
		
	}
	// 
	private function goldCondition(){
		$this->setAllEmployee();
	}

	//人事
	private function humanResourceCondition(){

		if ($this->isHrDeparment()) {
			$this->setAllEmployee();
		} else {
			$this->setDeparmentQuery();
		}
		
	}

	//人事经理
	private function hrMasterCondition(){
		$this->setAllEmployee();
	}

	//风控经理
	private function riskMasterCondition(){
		$this->setDepartmentEmployee();
	}
	//客服经理
	private function serviceMasterCondition(){
		$this->setDepartmentEmployee();
	}



	//部门经理
	private function departmentMasterCondition(){
		$this->setDepartmentEmployee();
	}


	public function setRoleCondition(){
		$this->roleEname = $this->getRoleEname();
        $funcName = $this->roleEname."Condition";
        if (method_exists($this, $funcName)) {
             call_user_func(array($this, $funcName));
        } else {
        	$this->error("没有权限EmployeeController");
        }
	}

	



	public function getRoles(){
		/*$row = M('rbac_role')->field('level')->find(session('account')['userInfo']['role_id']);
		return D('rbac_role')->where(array('level'=>array('gt', $row['level'])))->select();*/
		$departRow = D("Department")->find(session('account')['userInfo']['department_id']);
		if ($departRow) {
			return D("Department")->getEmployeeRoles($departRow['type']);
		} else {
			return D('rbac_role')->select();
		}
		
	}


	/**
	* 获取用户 角色 id
	*
	*/
	public function getUserRoles(){
		$result = D('rbac_role_user')->where(array('user_id'=> I('get.user_id',0)))->select();
		$this->ajaxReturn($result);
	}

	/**
	* 设置用户 角色 id
	*/
//原来的
	/*public function setUserRoles(){
		$M = D('rbac_role_user');
		$user_id = I('post.user_id',0);
		$role_ids = I('post.role_ids');
		$insert_list = array();
		if (is_array($role_ids)) {
			foreach ($role_ids as $value) {
				$insert_list[] = array('role_id'=>$value, 'user_id'=>$user_id);
			}
		} else {
			$insert_list[] = array('role_id'=>$role_ids, 'user_id'=>$user_id);
		}
		
		$M->startTrans(); 
		$result = $M->where(array('user_id'=>$user_id))->delete();

		if ($result !== false) {
			$insert_result = $M->addAll($insert_list);
			if ($insert_result !== false) {
				$re = M('user_info')->data(array('user_id'=>$user_id, 'role_id'=>$role_ids))->save();
				if ($re !== false) {
					$M->commit();
					$this->success("操作成功");
				} else {
					$M->rollback();
					$this->error("操作失败".M('user_info')->getError());
				}
				
			} else {
				$M->rollback();
				$this->error("操作失败".$M->getError());
			}
		} else {
			$M->rollback();
			
			$this->error("操作失败".$M->getError());
		}
	}*/
	public function setUserRoles(){
		$url = "User/setUserInfo";
	    $result = http_post($url,$_POST);

	}

	/**
	* 预处理
	*/
	public function _before_add(){
		$this->rightProcted();
		/*$user = new User;
		$user->getRoleObject();
		$user->setEmployeeAddData();*/

		//如果是人事部 则添加的员工部门为 0
		//如果不是人事部 则添加的员工部门为 当前员工的部门


		// $departmentRow = D('Department')->find($this->getDepartmentId());
		/*if ($this->isHrDeparment()) {
			$_POST['department_id'] = 0;
		} else {
			$_POST['department_id'] = session('account')['userInfo']['department_id'];
		}*/

		
	}

	/**
	* 添加
	*/
	public function add(){
		
		$url = "User/setUser";
	    $result = http_post($url,$_POST);
	    $result = json_decode($result,true);
	    if($result["status"]==0){
	    	$this->error($result["info"]);
	    }
	 

	}

	/**
	* 编辑
	*/
	public function edit(){
		$url = "User/setUserInfo";
	    $result = http_post($url,$_POST);
	      $result = json_decode($result,true);
	    if($result["status"]==0){
	    	$this->error($result["info"]);
	    }
		//新方法
  /*      $re = M('userInfo')->create($_POST, 2);
		if ($re) {
			if (M('userInfo')->where(array('user_id'=>I('post.id') ))->save() !== false) {
				$this->success(L('ADD_SUCCESS'));
			} else {
				$this->error($this->M->getError().$this->M->getLastSql());
			}
		} else {
			$this->error($this->M->getError().$this->M->getLastSql());
		}*/

	}
	//修改用户密码
	public function changePassword(){
	/*	echo 'ss';
		print_r($_POST);
		echo 'kk';*/
		$url = "User/changePassword";
	    $result = http_post($url,$_POST);
	}


	public function setEmployeeEntry(){
		$re = $this->M->create($_POST, 2);
		if ($re) {
			if ($this->M->save() !== false) {
				$this->success(L('ADD_SUCCESS'));
			} else {
				$this->error($this->M->getError().$this->M->getLastSql());
			}
		} else {
			$this->error($this->M->getError().$this->M->getLastSql());
		}
	}

	public function setEmployeeDepartemtnItem(){
		if ($this->getRoleEname()=='gold') {
			return 1;
		} else if(session('account')['userInfo']['department_id']!=0){
			$row = $this->getDepartmentRow();
			if ($row['type']==DepartmentModel::HR_DEPARTMENT) {
				return 1;
			} 
		}
		return 0;
	}

	/*public function _before_delete() {
		$this->setQeuryCondition();
	}*/
	/**
	*获取所选部门所包含小组
     */
    //原来的 不使用接口的
	/*public function getAllGroups(){
	    if(isset($_GET['department_id'])){
	        $arr=D('Group')->getAllGoups($_GET['department_id'],'id,name');
	        $this->ajaxReturn($arr);
        }
    }*/
    //使用接口 根据部门id获取小组信息
    public function getAllGroups(){
    	$department_id =I("get.department_id"); 
    	if(is_numeric($department_id)){
		    $field = "id,name";
		    $getgroupById_url = "Group/getGroupById?id=".$department_id."&field=".$field;
		    $crm_groups = json_decode(http_get($getgroupById_url),true);
		    $this->ajaxReturn($crm_groups);
    	}
    }

  
   

  

}
